<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of category_model
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Management_model extends CI_Model {

    //put your code here


    function __construct() {
        parent::__construct();
        log_message('debug', 'Init Model Initialized');
    }

//--Company List -----//    
    public function company_list($type='CAR', $num=NULL, $offset=NULL) {
        if (isset($num) && isset($offset)) {
            $sql = "select * from make where status=51 and type='$type' limit $offset, $num";
        } else {
            $sql = "select * from make where status=51  and type='$type' ";
        }
        $query = $this->db->query($sql);
        $result = $query->result();
        return $result;
    }

//--Company Update ----//    
    public function company_update($type='CAR', $id=NULL, $detail=NULL) {
        $data = array('make' => $detail);
        $this->db->where('id', $id);
        $this->db->where('type', $type);
        $this->db->update('make', $data);
    }

    public function company_delete($type='CAR', $id=NULL) {
        //-Make
        $sql = "Delete from make where id=$id and type='$type'";
        $this->db->query($sql);
        //-Model
        $sql = "Delete from model where id=$id and type='$type'";
        $this->db->query($sql);
        //-View
        $sql = "Delete from version where id=$id and type='$type'";
        $this->db->query($sql);
    }

    public function company_insert($type='CAR', $detail=NULL) {
        $data = array('make' => $detail, 'type' => $type);
        $this->db->insert('make', $data);
    }

    public function model_list($type='CAR', $num=NULL, $offset=NULL, $id=NULL) {
        $sql = "select * from model where status=51 and make_id=$id and type='$type' limit $offset, $num";
        $query = $this->db->query($sql);
        $result = $query->result();
        return $result;
    }

    public function model_update($type='CAR', $id=NULL, $detail=NULL) {
        $data = array('model' => $detail);
        $this->db->where('id', $id);
        $this->db->where('type', $type);
        $this->db->update('model', $data);
    }

    public function model_insert($type='CAR', $detail=NULL, $id=NULL) {
        $data = array('model' => $detail, 'make_id' => $id, 'type' => $type);
        $this->db->insert('model', $data);
    }

    public function model_delete($type='CAR', $id=NULL) {
        $this->db->where('id', $id);
        $this->db->where('type', $type);
        $this->db->delete('model');
        $this->db->where('model_id', $id);
        $this->db->where('type', $type);
        $this->db->delete('version');
    }

    public function version_list($type='CAR', $num=NULL, $offset=NULL, $id=NULL) {
        $sql = "select * from version where status=51 and model_id=$id and type='$type' limit $offset, $num";
        $query = $this->db->query($sql);
        $result = $query->result();
        return $result;
    }

    public function version_update($type='CAR', $id=NULL, $detail=NULL) {
        $data = array('version' => $detail);
        $this->db->where('id', $id);
        $this->db->where('type', $type);
        $this->db->update('version', $data);
    }

    //--Version Description Update (Subha-07/08/2012)--/
    function version_description_update($type='CAR', $post_data) {
        if ($post_data) {
            $this->db->where('type', $type);
            $this->db->where('make_id', $post_data['make_id']);
            $this->db->where('model_id', $post_data['model_id']);
            $this->db->where('id', $post_data['id']);
            $this->db->update('version', $post_data);
        }
    }

    public function version_delete($type='CAR', $id=NULL) {
        $this->db->where('id', $id);
        $this->db->where('type', $type);
        $this->db->delete('version');
    }

    public function version_insert($type='CAR', $detail=NULL, $id=NULL, $company_id=NULL) {
        $data = array('version' => $detail, 'model_id' => $id, 'make_id' => $company_id, 'type' => $type);
        $this->db->insert('version', $data);
    }

    function main_features() {
        $sql = "select * from pickup where slno=0 and status=51";
        $query = $this->db->query($sql);
        $result = $query->result();
        return $result;
    }

    function feature_list($type='CAR', $code=NULL) {
        $sql = "select * from pickup where slno!=0 and code=$code and type='$type' and status=51";
        $query = $this->db->query($sql);
        $result = $query->result();
        return $result;
    }

//--Car Maker detail --//    
    function get_make($type='CAR', $make_id=NULL) {
        if ($make_id) {
            $code = trim($make_id);
            $sql = "Select * from make where id='$code' and type='$type'";
            $query = $this->db->query($sql);
            $row = $query->row();
            if ($query->num_rows == 1)
                return $row;
        }
    }

    function get_model($type='CAR', $model_id=NULL) {
        $sql = "Select * from model where id='$model_id' and type='$type'";
        $query = $this->db->query($sql);
        $row = $query->row();
        if ($query->num_rows == 1)
            return $row;
    }

//--Version Name (Subha-03/08/2012)     
    function get_version($type='CAR', $version_id=NULL) {
        if ($version_id) {
            $sql = "Select * from version where id='$version_id' and type='$type'";
            $query = $this->db->query($sql);
            $row = $query->row();
            if ($query->num_rows == 1)
                return $row;
        }
    }

//-- Get car version features --Subha-03/08/2012//
    function get_features($type='CAR', $code) {
        if ($code) {
            $sql = "select * from feature where slno <> 0 and code = $code and type='$type' and status = 51 order by feature asc";
            $query = $this->db->query($sql);
            return $query->result();
        }
    }
    function get_interiour_by_id($type='CAR', $code=NULL, $version=NULL) {
        if ($code) {
            $sql = "select * from feature where code=$code and type='$type' and slno in(select slno from features_master where version=$version and code=$code) order by feature asc";
            $query = $this->db->query($sql);
            return $query->result();
        }
    }

// ------Joy 11/08/2012-----
    function get_specifications($type=NULL) {
        $sql = "select * from specification where slno <> 0 and type='$type' and status = 51 order by specification asc";
        $query = $this->db->query($sql);
        return $query->result();
    }
    function get_specification_code_by_version_id($type=NULL, $versionID=NULL) {
        $sql = "select code from specification_master where version=$versionID group by code";
        $query = $this->db->query($sql);
        return $query->result();
    }
    function get_specification_by_version_id($type=NULL, $versionID=NULL, $code=NULL) {
        $slno = "";
        $sql = "select slno from specification_master where version=$versionID and code=$code order by code asc" ;
        $query = $this->db->query($sql);
        $result = $query->result();
        foreach ($result as $result):
            $slno.=$result->slno.',';
        endforeach;
        $slno = rtrim($slno, ",");
        $sql = "select specification,slno from specification where slno in($slno) and code=$code";
        $query = $this->db->query($sql);
        return $query->result();
    }
    function get_specification_value($type='CAR',$slno=NULL, $code=NULL)
    {
       $sql = "Select specification_value from specification_master where slno='$slno' and code='$code' and type='$type'";
        $query = $this->db->query($sql);
        $row = $query->row();
        if ($query->num_rows == 1)
            return $row->specification_value; 
    }
     function specification_type($code=NULL)
    {
        $sql = "Select * from specification where slno='0' and code='$code'";
        $query = $this->db->query($sql);
        $row = $query->row();
        if ($query->num_rows == 1)
            return $row->specification;
        
    }
    function get_make_image($type='CAR', $make_id=NULL) {
        $sql = "Select * from make where id='$make_id' and type='$type'";
        $query = $this->db->query($sql);
        $row = $query->row();
        if ($query->num_rows == 1)
            return $row->image;
    }

    function get_version_image($type='CAR', $version_id=NULL) {
        $sql = "Select * from version where id='$version_id' and type='$type'";
        $query = $this->db->query($sql);
        $row = $query->row();
        if ($query->num_rows == 1)
            return $row->image;
    }
//-- End of Code --//    
}